﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyHealthOnline.Dal.Generated.EntityClasses;
using MyHealthOnline.Business;
using MyHealthOnline.Dal.Generated.CollectionClasses;

namespace MyHealthOnline.Patient
{
    public partial class QuestionList : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private QuestionCollection dataSource;
        public QuestionCollection DataSource
        {
            get
            {
                return ViewState["DataSource"] as QuestionCollection;
            }
            set
            {
                ViewState["DataSource"] = value;
                rptQuestions.DataSource = value;
                rptQuestions.DataBind();
            }
        }

        public bool IsOpenQuestion(string type)
        {
            return (type == "Open");
        }

        public bool IsMultiChoiceQuestion(string type)
        {
            return (type == "Multi");
        }

        public bool IsDateQuestion(string type)
        {
            return (type == "Date");
        }

        public void SaveAnswers()
        {
            foreach (RepeaterItem item in rptQuestions.Items)
            {
                Panel pnlOpen = item.FindControl("pnlOpenQuestion") as Panel;
                if (pnlOpen != null && pnlOpen.Visible)
                {
                    TextBox txtOpen = pnlOpen.FindControl("txtOpenQuestion") as TextBox;
                    string answer = txtOpen.Text;

                    OpenQuestionEntity openQuestion =
                        DataSource.ElementAt(item.ItemIndex).OpenQuestion;
                    QuestionBusiness.UpdateOpenQuestion(openQuestion, answer);

                }
                else
                {
                    Panel pnlMulti = item.FindControl("pnlMultiChoiceQuestion") as Panel;
                    if (pnlMulti != null && pnlMulti.Visible)
                    {
                        RadioButtonList rbl = pnlMulti.FindControl("rblMultiChoiceQuestion") as RadioButtonList;
                        string answer = rbl.SelectedItem.Text;

                        TextBox txtRemarks = pnlMulti.FindControl("txtRemarks") as TextBox;
                        string remarks = txtRemarks.Text;

                        MultiChoiceQuestionEntity multiChoiceQuestion =
                            DataSource.ElementAt(item.ItemIndex).MultiChoiceQuestion;
                        QuestionBusiness.UpdateMultiQuestion(multiChoiceQuestion, answer, remarks);
                    }
                    else
                    {
                        Panel pnlDate = item.FindControl("pnlDateQuestion") as Panel;
                        if (pnlDate != null && pnlDate.Visible)
                        {
                            TextBox cal = pnlDate.FindControl("txtDateQuestion") as TextBox;
                            DateTime answer = Convert.ToDateTime( cal.Text);

                            DateQuestionEntity dateQuestion =
                                DataSource.ElementAt(item.ItemIndex).DateQuestion;

                            QuestionBusiness.UpdateDateQuestion(dateQuestion, answer);
                        }
                    }
                }
            }
       }

        protected void rptQuestions_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                QuestionEntity question = e.Item.DataItem as QuestionEntity;
                if (question.Type == "Open")
                {
                    Label lblOpenQuestion = e.Item.FindControl("lblOpenQuestion") as Label;
                    lblOpenQuestion.Text = question.QuestionText;
                }
                else if (question.Type == "Multi")
                {
                    Label lblMultiQuestion = e.Item.FindControl("lblMultiQuestion") as Label;
                    lblMultiQuestion.Text = question.QuestionText;

                    RadioButtonList rblMultiChoiceQuestion = e.Item.FindControl("rblMultiChoiceQuestion") as RadioButtonList;
                    string choices=question.MultiChoiceQuestion.Choices;
                    rblMultiChoiceQuestion.DataSource = choices.Split(';');
                    rblMultiChoiceQuestion.DataBind();
                }
                else if (question.Type == "Date")
                {
                    Label lblDateQuestion = e.Item.FindControl("lblDateQuestion") as Label;
                    lblDateQuestion.Text = question.QuestionText;
                }
            }
        }
    }
}